package de.hdu.pms.ho.ctrl;


import javax.servlet.*;
import javax.servlet.http.*;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;

import org.apache.log4j.Logger;
import org.hibernate.SQLQuery;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.classic.Session;
import de.hdu.pms.ho.model.Kunde;

import java.io.*;
import java.util.*;

/**
  * Catches a {@link Kunde} from the database and takes it into PDF list.
  * The list will be open in the browser. 
  * For neither information look into green comments in {@link Kunden2pdf} class.
  * 
  * @author Hendrik Neumann
 */
public class Kunden2pdf extends HttpServlet {

	private static Logger Log = Logger.getLogger(Kunden2pdf.class);

	private static final long serialVersionUID = 1L;
	
	// Starts servlet function
	public void init(ServletConfig config) throws ServletException{
		super.init(config);
	}
	
	public void doGet(HttpServletRequest request, 
			HttpServletResponse response) 
			throws ServletException, IOException{
		doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, 
			HttpServletResponse response) 
			throws ServletException, IOException{
		
		SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory();
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		
		// Starts a SQL query catching all Kunden from database
		SQLQuery c = session.createSQLQuery("select * from kunde order by name, vorname");
		c.addEntity("kunden", Kunde.class);
		@SuppressWarnings("unchecked")
		
		// Takes all Kunden into a list field
		List<Kunde> alleKunden = c.list(); 
		
		response.setContentType("application/pdf"); 
		Log.debug("application/pdf");
		// New PDF document in a4 paper 
		Document document = new Document(PageSize.A4.rotate());

		try{

			// Gets a new instance of the PDF document writer
			PdfWriter.getInstance(document, 
				response.getOutputStream()); 
			document.open();
			
			document.newPage();
			// Meta data
			document.addTitle("HotelOrganizerKundenliste");
			document.addAuthor("HotelOrganizerAdministrator");
			document.addCreator("Hendrik Neumann");
			document.addCreationDate();

			// Font style
			Font font = FontFactory.getFont ( FontFactory.TIMES_BOLD,20 ) ;
			Font headerfont = FontFactory.getFont(FontFactory.TIMES_BOLD, 12);
			headerfont.setColor(BaseColor.WHITE);
			Font cellfont = FontFactory.getFont(FontFactory.TIMES, 10);
			
			document.add(new Chunk ("\n" + "Kundendaten", font));

			// Is used for formating content
			document.add(new Paragraph(" "));

			// Creates table with 7 columns
			PdfPTable table = new PdfPTable(7);
			
			// Properties of the table
			table.setWidthPercentage(100);
			table.setHorizontalAlignment(Element.ALIGN_LEFT);
			table.setTotalWidth(new float[]{ 30, 25, 10, 20, 25, 15, 15});
						
			table.getDefaultCell().setBackgroundColor(BaseColor.BLACK);
			table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);

				table.addCell(new Phrase ("Name, Vorname", headerfont));
				table.addCell(new Phrase ("Straße", headerfont));
				table.addCell(new Phrase ("PLZ", headerfont));
				table.addCell(new Phrase ("Ort", headerfont));
				table.addCell(new Phrase ("Telefonnummer", headerfont));
				table.addCell(new Phrase ("Kontonummer", headerfont));
				table.addCell(new Phrase ("Bankleitzahl", headerfont));
							
				Boolean grey = false;
			
				table.getDefaultCell().setBackgroundColor(null);
				table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
			
			// Loops all Kunden from database into PDF table 
			for (int i = 0; i < alleKunden.size() ; i++){			
								
				// Different color in the rows of table
				if (grey){
					table.getDefaultCell().setBackgroundColor(BaseColor.LIGHT_GRAY);
					grey = false;
				}
				else {
					table.getDefaultCell().setBackgroundColor(BaseColor.WHITE);
					grey = true;
				}
		
				// Filling table with data from loop (increment function)
				table.addCell(new Phrase (alleKunden.get(i).getName() + ", " + alleKunden.get(i).getVorname(), cellfont));
				table.addCell(new Phrase (alleKunden.get(i).getStrasse(), cellfont));
				table.addCell(new Phrase (alleKunden.get(i).getPlz(), cellfont));
				table.addCell(new Phrase (alleKunden.get(i).getOrt(), cellfont));
				table.addCell(new Phrase (alleKunden.get(i).getTelefonnummer(), cellfont));
				table.addCell(new Phrase (alleKunden.get(i).getktonrString(), cellfont));
				table.addCell(new Phrase (alleKunden.get(i).getblzString(), cellfont));

			}
		
			// Footer informations
			document.add(table);
			document.add(new Paragraph (" "));
			document.add(new Chunk("\n" + "HotelOrganizer - Ein Projekt im Rahmen der PMS Vorlesung " + "\n" + new Date() +
					"\n" + "\n" + "Dominic Feig, Helena Semmel, Hendrik Neumann, Thomas Niedermeier", cellfont));
	
			document.close(); 
		}catch(DocumentException e){
			e.printStackTrace();
		}
	}
	
}

	